package fr.utbm.lo53;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class mobileRequest
 */
@WebServlet("/mobileRequest")
public class mobileRequest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public mobileRequest() {
        super();
        /*UDPHandler udpHandler = new UDPHandler();
        udpHandler.start();*/
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		if (request.getParameter("MEASURE") != null) {	
			DatagramSocket SocketUDP = new DatagramSocket(6666);
            SocketUDP.setBroadcast(true);
			double x = Double.parseDouble(request.getParameter("X"));
			double y = Double.parseDouble(request.getParameter("Y"));
			int mapId = Integer.parseInt(request.getParameter("mid"));
			String addr = request.getRemoteAddr();
			out.println("a=" + addr);
			out.println("address ip =" + Network.getMacByIp(addr));
			out.println("addr broadcast=" + Network.getBroadcastAddr());
			String mac = Network.getMacByIp(addr);
			requestRssiPacket packet = new requestRssiPacket(Network.getBroadcastAddr(),6666, x, y, mac, mapId);
			SocketUDP.send(packet.getPacket());
			out.println("packet send");
			SocketUDP.close();
		}
		
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
